home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 1999 #5 / 1999 CD 5 (black).iso / Delphi3 / install / data.z / PICEDIT.INT < prev    next >
Encoding:
Text File  |  1997-08-05  |  8.5 KB  |  317 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Delphi Visual Component Library                 }
  5. {                                                       }
  6. {       Copyright (c) 1995 Borland International        }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit PicEdit;
  11.  
  12. interface
  13.  
  14. uses Windows, Classes, Graphics, Forms, Controls, Dialogs, Buttons, DsgnIntf,
  15.   StdCtrls, ExtCtrls, ExtDlgs;
  16.  
  17. type
  18.   TPictureEditorDlg = class(TForm)
  19.     OpenDialog: TOpenPictureDialog;
  20.     SaveDialog: TSavePictureDialog;
  21.     OKButton: TButton;
  22.     CancelButton: TButton;
  23.     HelpButton: TButton;
  24.     GroupBox1: TGroupBox;
  25.     ImagePanel: TPanel;
  26.     Load: TButton;
  27.     Save: TButton;
  28.     Clear: TButton;
  29.     ImagePaintBox: TPaintBox;
  30.     procedure FormCreate(Sender: TObject);
  31.     procedure FormDestroy(Sender: TObject);
  32.     procedure LoadClick(Sender: TObject);
  33.     procedure SaveClick(Sender: TObject);
  34.     procedure ClearClick(Sender: TObject);
  35.     procedure HelpButtonClick(Sender: TObject);
  36.     procedure ImagePaintBoxPaint(Sender: TObject);
  37.   private
  38.     Pic: TPicture;
  39.   end;
  40.  
  41.   TPictureEditor = class(TComponent)
  42.   private
  43.     FGraphicClass: TGraphicClass;
  44.     FPicture: TPicture;
  45.     FPicDlg: TPictureEditorDlg;
  46.     procedure SetPicture(Value: TPicture);
  47.   public
  48.     constructor Create(AOwner: TComponent); override;
  49.     destructor Destroy; override;
  50.     function Execute: Boolean;
  51.     property GraphicClass: TGraphicClass read FGraphicClass write FGraphicClass;
  52.     property Picture: TPicture read FPicture write SetPicture;
  53.   end;
  54.  
  55. { TPictureProperty
  56.   Property editor the TPicture properties (e.g. the Picture property).  Brings
  57.   up a file open dialog allowing loading a picture file. }
  58.  
  59.   TPictureProperty = class(TPropertyEditor)
  60.   public
  61.     procedure Edit; override;
  62.     function GetAttributes: TPropertyAttributes; override;
  63.     function GetValue: string; override;
  64.     procedure SetValue(const Value: string); override;
  65.   end;
  66.  
  67. { TGraphicProperty }
  68.  
  69.   TGraphicProperty = class(TClassProperty)
  70.   public
  71.     procedure Edit; override;
  72.     function GetAttributes: TPropertyAttributes; override;
  73.     function GetValue: string; override;
  74.     procedure SetValue(const Value: string); override;
  75.   end;
  76.  
  77. { TGraphicEditor }
  78.  
  79.   TGraphicEditor = class(TDefaultEditor)
  80.   public
  81.     procedure EditProperty(PropertyEditor: TPropertyEditor;
  82.       var Continue, FreeEditor: Boolean); override;
  83.   end;
  84.  
  85. implementation
  86.  
  87. uses TypInfo, SysUtils, LibConst, LibHelp;
  88.  
  89. {$R *.DFM}
  90.  
  91. { TPictureEditorDlg }
  92.  
  93. procedure TPictureEditorDlg.FormCreate(Sender: TObject);
  94. begin
  95.   HelpContext := hcDPictureEditor;
  96.   Pic := TPicture.Create;
  97.   Save.Enabled := False;
  98. end;
  99.  
  100. procedure TPictureEditorDlg.FormDestroy(Sender: TObject);
  101. begin
  102.   Pic.Free;
  103. end;
  104.  
  105. procedure TPictureEditorDlg.LoadClick(Sender: TObject);
  106. begin
  107.   OpenDialog.Title := SLoadPictureTitle;
  108.   if OpenDialog.Execute then
  109.   begin
  110.     Pic.LoadFromFile(OpenDialog.Filename);
  111.     ImagePaintBox.Invalidate;
  112.     Save.Enabled := (Pic.Graphic <> nil) and not Pic.Graphic.Empty;
  113.     Clear.Enabled := (Pic.Graphic <> nil) and not Pic.Graphic.Empty;
  114.   end;
  115. end;
  116.  
  117. procedure TPictureEditorDlg.SaveClick(Sender: TObject);
  118. begin
  119.   if Pic.Graphic <> nil then
  120.   begin
  121.     SaveDialog.Title := SSavePictureTitle;
  122.     with SaveDialog do
  123.     begin
  124.       DefaultExt := GraphicExtension(TGraphicClass(Pic.Graphic.ClassType));
  125.       Filter := GraphicFilter(TGraphicClass(Pic.Graphic.ClassType));
  126.       if Execute then Pic.SaveToFile(Filename);
  127.     end;
  128.   end;
  129. end;
  130.  
  131. procedure TPictureEditorDlg.ImagePaintBoxPaint(Sender: TObject);
  132. var
  133.   DrawRect: TRect;
  134.   SNone: string;
  135. begin
  136.   with TPaintBox(Sender) do
  137.   begin
  138.     Canvas.Brush.Color := {Self.}Color;
  139.     DrawRect := ClientRect;//Rect(Left, Top, Left + Width, Top + Height);
  140.     if Pic.Width > 0 then
  141.     begin
  142.       with DrawRect do
  143.         if (Pic.Width > Right - Left) or (Pic.Height > Bottom - Top) then
  144.         begin
  145.           if Pic.Width > Pic.Height then
  146.             Bottom := Top + MulDiv(Pic.Height, Right - Left, Pic.Width)
  147.           else
  148.             Right := Left + MulDiv(Pic.Width, Bottom - Top, Pic.Height);
  149.           Canvas.StretchDraw(DrawRect, Pic.Graphic);
  150.         end
  151.         else
  152.           with DrawRect do
  153.             Canvas.Draw(Left + (Right - Left - Pic.Width) div 2, Top + (Bottom - Top -
  154.               Pic.Height) div 2, Pic.Graphic);
  155.     end
  156.     else
  157.       with DrawRect, Canvas do
  158.       begin
  159.         SNone := srNone;
  160.         TextOut(Left + (Right - Left - TextWidth(SNone)) div 2, Top + (Bottom -
  161.           Top - TextHeight(SNone)) div 2, SNone);
  162.       end;
  163.   end;
  164. end;
  165.  
  166. procedure TPictureEditorDlg.ClearClick(Sender: TObject);
  167. begin
  168.   Pic.Graphic := nil;
  169.   ImagePaintBox.Invalidate;
  170.   Save.Enabled := False;
  171.   Clear.Enabled := False;
  172. end;
  173.  
  174. { TPictureEditor }
  175.  
  176. constructor TPictureEditor.Create(AOwner: TComponent);
  177. begin
  178.   inherited Create(AOwner);
  179.   FPicture := TPicture.Create;
  180.   FPicDlg := TPictureEditorDlg.Create(Self);
  181.   FGraphicClass := TGraphic;
  182. end;
  183.  
  184. destructor TPictureEditor.Destroy;
  185. begin
  186.   FPicture.Free;
  187.   inherited Destroy;
  188. end;
  189.  
  190. function TPictureEditor.Execute: Boolean;
  191. begin
  192.   FPicDlg.Pic.Assign(FPicture);
  193.   with FPicDlg.OpenDialog do
  194.   begin
  195.     Options := [ofHideReadOnly, ofFileMustExist, ofShowHelp];
  196.     DefaultExt := GraphicExtension(GraphicClass);
  197.     Filter := GraphicFilter(GraphicClass);
  198.     HelpContext := hcDLoadPicture;
  199.   end;
  200.   with FPicDlg.SaveDialog do
  201.   begin
  202.     Options := [ofHideReadOnly, ofFileMustExist, ofShowHelp];
  203.     DefaultExt := GraphicExtension(GraphicClass);
  204.     Filter := GraphicFilter(GraphicClass);
  205.     HelpContext := hcDSavePicture;
  206.   end;
  207.   FPicDlg.Save.Enabled := (FPicture.Graphic <> nil) and not FPicture.Graphic.Empty;
  208.   FPicDlg.Clear.Enabled := (FPicture.Graphic <> nil) and not FPicture.Graphic.Empty;
  209.   Result := FPicDlg.ShowModal = mrOK;
  210.   if Result then FPicture.Assign(FPicDlg.Pic);
  211. end;
  212.  
  213. procedure TPictureEditor.SetPicture(Value: TPicture);
  214. begin
  215.   FPicture.Assign(Value);
  216. end;
  217.  
  218. { TPictureProperty }
  219.  
  220. procedure TPictureProperty.Edit;
  221. var
  222.   PictureEditor: TPictureEditor;
  223. begin
  224.   PictureEditor := TPictureEditor.Create(nil);
  225.   try
  226.     PictureEditor.Picture := TPicture(Pointer(GetOrdValue));
  227.     if PictureEditor.Execute then
  228.       SetOrdValue(Longint(PictureEditor.Picture));
  229.   finally
  230.     PictureEditor.Free;
  231.   end;
  232. end;
  233.  
  234. function TPictureProperty.GetAttributes: TPropertyAttributes;
  235. begin
  236.   Result := [paDialog];
  237. end;
  238.  
  239. function TPictureProperty.GetValue: string;
  240. var
  241.   Picture: TPicture;
  242. begin
  243.   Picture := TPicture(GetOrdValue);
  244.   if Picture.Graphic = nil then
  245.     Result := srNone else
  246.     Result := '(' + Picture.Graphic.ClassName + ')';
  247. end;
  248.  
  249. procedure TPictureProperty.SetValue(const Value: string);
  250. begin
  251.   if Value = '' then SetOrdValue(0);
  252. end;
  253.  
  254. { TGraphicProperty }
  255.  
  256. procedure TGraphicProperty.Edit;
  257. var
  258.   PictureEditor: TPictureEditor;
  259. begin
  260.   PictureEditor := TPictureEditor.Create(nil);
  261.   try
  262.     PictureEditor.GraphicClass := TGraphicClass(GetTypeData(GetPropType)^.ClassType);
  263.     PictureEditor.Picture.Graphic := TGraphic(Pointer(GetOrdValue));
  264.     if PictureEditor.Execute then
  265.       if (PictureEditor.Picture.Graphic = nil) or
  266.          (PictureEditor.Picture.Graphic is PictureEditor.GraphicClass) then
  267.         SetOrdValue(LongInt(PictureEditor.Picture.Graphic))
  268.       else
  269.         raise Exception.Create(SInvalidFormat);
  270.   finally
  271.     PictureEditor.Free;
  272.   end;
  273. end;
  274.  
  275. function TGraphicProperty.GetAttributes: TPropertyAttributes;
  276. begin
  277.   Result := [paDialog];
  278. end;
  279.  
  280. function TGraphicProperty.GetValue: string;
  281. var
  282.   Graphic: TGraphic;
  283. begin
  284.   Graphic := TGraphic(GetOrdValue);
  285.   if (Graphic = nil) or Graphic.Empty then
  286.     Result := srNone else
  287.     Result := '(' + Graphic.ClassName + ')';
  288. end;
  289.  
  290. procedure TGraphicProperty.SetValue(const Value: string);
  291. begin
  292.   if Value = '' then SetOrdValue(0);
  293. end;
  294.  
  295. { TPictureEditor }
  296.  
  297. procedure TGraphicEditor.EditProperty(PropertyEditor: TPropertyEditor;
  298.   var Continue, FreeEditor: Boolean);
  299. var
  300.   PropName: string;
  301. begin
  302.   PropName := PropertyEditor.GetName;
  303.   if (CompareText(PropName, 'PICTURE') = 0) or
  304.     (CompareText(PropName, 'IMAGE') = 0) then
  305.   begin
  306.     PropertyEditor.Edit;
  307.     Continue := False;
  308.   end;
  309. end;
  310.  
  311. procedure TPictureEditorDlg.HelpButtonClick(Sender: TObject);
  312. begin
  313.   Application.HelpContext(HelpContext);
  314. end;
  315.  
  316. end.
  317.